var pathSum = function (root, sum) {
  if (!root) return [];
  let res = [];
  const getPath = (root, sum, cur = []) => {
    if (root) {
      cur.push(root.val);
      sum -= root.val;
      if (!root.left && !root.right && sum === 0) { 
        res.push(cur.slice());
      }
      getPath(root.left, sum, cur);
      getPath(root.right, sum, cur);
      cur.pop(); 
    }
  }
  getPath(root, sum);
  return res;
}